package com.example.javafx.controller


import com.example.javafx.bilibilidownload.constant.Myinfo.default_begin_time
import com.example.javafx.bilibilidownload.constant.Myinfo.default_min_play
import com.example.javafx.bilibilidownload.service.BiliServiceImpl
import com.example.javafx.bilibilidownload.util.CacheUtil
import javafx.application.Platform
import javafx.event.ActionEvent
import javafx.fxml.FXML
import javafx.fxml.FXMLLoader
import javafx.scene.Parent
import javafx.scene.control.*
import javafx.scene.image.Image
import javafx.scene.image.ImageView
import javafx.scene.layout.BorderPane
import javafx.scene.layout.VBox
import javafx.scene.shape.Circle
import java.util.*

class MainController {

    @FXML
    lateinit var centerBorderPane: BorderPane

    @FXML
    lateinit var leftPane: VBox

    @FXML
    lateinit var logoImages: ImageView

    @FXML
    lateinit var refresh: Button

    @FXML
    lateinit var setting: Button

    @FXML
    lateinit var home: Button

    @FXML
    lateinit var toolBar: ToolBar

    var bilibiliService = BiliServiceImpl()

    @FXML
    fun initialize() {
        initializeTree()
    }

    fun initializeTree() {

        var image = Image(javaClass.getResourceAsStream("/img/logo.jpg"))
        logoImages.image = image
        logoImages.clip = Circle(50.0, 50.0, 50.0)


    }

    @FXML
    @Throws(Exception::class)
    fun refresh(event: ActionEvent) {

        var begin = System.currentTimeMillis()
        // 创建根节点
        var rootItem = TreeItem<Any>("Root")
        // 展开根节点
        rootItem.isExpanded = true

        // 开始解析我的关注
        var myFocus = TreeItem<Any>("我的关注")
        // 获取关注分组
        var myfocus = bilibiliService.getMyfocus();
        myfocus.forEach {
            var focusItem = TreeItem<Any>(it)
            // 获取每个分组下的Up主
            val focusUpList = bilibiliService.getFocusUpList(it.focusName)
            focusUpList.forEach {
                var upItem = TreeItem<Any>(it)
                // 获取up的所有视频
                val upAllVideoList = bilibiliService.getUPAllVideoList(it.upName, default_min_play, default_begin_time, emptyList())
                upAllVideoList.forEach {
                    var videoItem = TreeItem<Any>(it)
                    upItem.children.add(videoItem)
                }
                focusItem.children.add(upItem)
            }
            myFocus.children.add(focusItem)

        }

        var end1 = System.currentTimeMillis()
        println("刷新我的关注消耗时间为${(end1 - begin)}")

        var myFavorite = TreeItem<Any>("我的收藏夹")
        var myfavorite = bilibiliService.getMyFavorite()
        myfavorite.forEach {
            var favoriteItem = TreeItem<Any>(it)
            myFavorite.children.add(favoriteItem)
            val favoriteVideoList = bilibiliService.getMyFavoriteDetail(it)
            favoriteVideoList.forEach {
                var videoItem = TreeItem<Any>(it)
                favoriteItem.children.add(videoItem)

                // 如果是合集视频，则要拿到合集列表
                if (!it.collectionName.isNullOrEmpty()) {
                    val collectionVideoList = bilibiliService.getMyFavoriteCollectionDetail(it)
                    collectionVideoList.forEach {
                        var collectVideoItem = TreeItem<Any>(it)
                        videoItem.children.add(collectVideoItem)
                    }
                }
            }
        }

        var end2 = System.currentTimeMillis()
        println("刷新我的收藏夹消耗时间为${(end2 - end1)}")

        var singleDownload = TreeItem<Any>("单独下载")

        rootItem.children.add(myFocus)
        rootItem.children.add(myFavorite)
        rootItem.children.add(singleDownload)

        val homeController = CacheUtil.getFxmlController<HomeController>(HomeController::class.java, "/fxml/home.fxml")
        homeController.tree.isShowRoot = false
        Platform.runLater {
            homeController.tree.root = rootItem
            centerBorderPane.center = homeController.rootPane
        }
    }

    @FXML
    @Throws(Exception::class)
    fun home(actionEvent: ActionEvent) {
        val controller = CacheUtil.getFxmlController<HomeController>(HomeController::class.java, "/fxml/home.fxml")
        centerBorderPane.center = controller.rootPane
    }

    @FXML
    @Throws(Exception::class)
    fun setting(event: ActionEvent) {
        val controller = CacheUtil.getFxmlController<SettingController>(SettingController::class.java, "/fxml/setting.fxml")
        centerBorderPane.center = controller.rootPane
    }
}
